//
// Copyright (c) 2010 xiaoningyb (xiaoningyb@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef TINYSHARING_MESSAGE_FORMAT_H
#define TINYSHARING_MESSAGE_FORMAT_H

#include <string>
#include <vector>
#include "sharing_config.h"

namespace tinysharing
{
    enum sharing_message_type
    {
        request_message,
        reply_message
    };    
    
    // The message Header
    struct sharing_message_header
    {
        //The magic word at the begin of messages.
        std::size_t magic_word;

        //Indicates the message version
        int message_version;
        
        //Indicates the byte ordering. A value of false indicates big-endian byte ordering,
        //and true indecates litte-endian
        bool byte_order;

        //The message type for entire message.(request, reply)
        sharing_message_type message_type;
        
        //The message length for entire message not include the message header
        std::size_t message_size;

        //The serial number stand for a request and a/multi reply(ies) session.
        std::size_t serial_number;

        sharing_message_header() : magic_word(tinysharing_magic_word),
                                   message_version(tinysharing_message_version),
                                   byte_order(__isbigendian()),
                                   message_type(request_message),
                                   message_size(0),
                                   serial_number(0)
        {
        }

        ~sharing_message_header()
        {
        }
        
        template <typename Archive>
        void serialize(Archive& ar, const unsigned int version)
        {
            ar & magic_word;
            ar & message_version;
            ar & byte_order;
            ar & message_type;
            ar & message_size;
            ar & serial_number;
        }
    };

    const std::size_t sharing_message_header_length = sizeof(sharing_message_header);    

    // A message be sent between server and client
    struct sharing_message
    {
        std::string command;
        std::string content;

        template <typename Archive>
        void serialize(Archive& ar, const unsigned int version)
        {
            ar & command;
            ar & content;            
        }
    };
    
} // namespace tinysharing

#endif // TINYSHARING_MESSAGE_FORMAT_H
